package xinyu.customer.web;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import xinyu.customer.R;
import xinyu.customer.base.AppBaseActivity;

public class WebActivity extends AppBaseActivity {
    protected XinYangWebView mWebView;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_right)
    TextView tvRight;
    @BindView(R.id.fl_title)
    RelativeLayout flTitle;
    @BindView(R.id.ll_root)
    LinearLayout mLinearRoot;
    @BindView(R.id.progressBar)
    ProgressBar mProgressBar;
    @BindView(R.id.fl_content)
    FrameLayout flContent;

    private boolean isWebViewFinish;
    private String mUrl;
    private String mTitle;

    public static void show(Context context, String url, String title) {
        if (TextUtils.isEmpty(url)) {
            return;
        }
        Intent intent = new Intent(context, WebActivity.class);
        intent.putExtra("url", url);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (!TextUtils.isEmpty(title)) {
            intent.putExtra("title", title);
        }
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        mUrl = getIntent().getStringExtra("url");
        mTitle = getIntent().getStringExtra("title");
        initWidget();
    }

    private void initWidget() {
        tvTitle.setText(TextUtils.isEmpty(mTitle) ? "返回" : mTitle);
        ivBack.setOnClickListener((v -> {
            if (mWebView != null && mWebView.canGoBack()) {
                mWebView.goBack();
            } else {
                finish();
            }
        }));

        mWebView = new XinYangWebView(this);
        mWebView.setBackgroundColor(getResources().getColor(R.color.secondary_background_color));
        mWebView.enableZoom();
        mWebView.enableCache();
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0);
        params.weight = 1;
        mWebView.setVisibility(View.INVISIBLE);
        mWebView.setLayoutParams(params);
        mLinearRoot.addView(mWebView);
        mWebView.setOnFinishFinish(new XinYangWebView.OnFinishListener() {
            @Override
            public void onReceivedTitle(String title) {
                setTitle(title);
            }

            @Override
            public void onProgressChange(int progress) {
                if (isDestroyed()) {
                    return;
                }
                mProgressBar.setProgress(progress);
                if (!mWebView.hasRule()) {
                    mWebView.setVisibility(View.VISIBLE);
                    return;
                }
                if (progress >= 60 && !isWebViewFinish) {
                    isWebViewFinish = true;
                    mWebView.postDelayed(() -> {
                        if (isDestroyed()) {
                            return;
                        }
                        mWebView.setVisibility(View.VISIBLE);
                    }, 300);
                }
            }

            @Override
            public void onError() {

            }

            @Override
            public void onFinish() {
                if (isDestroyed()) {
                    return;
                }
                mProgressBar.setVisibility(View.INVISIBLE);
            }
        });
        // 打开网页
        mWebView.loadUrl(mUrl);
    }

    @Override
    public void onBackPressed() {
        if (mWebView != null && mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mWebView != null) {
            mWebView.onDestroy();
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 这是一个监听用的按键的方法，keyCode 监听用户的动作，如果是按了返回键，同时Webview要返回的话，WebView执行回退操作，因为mWebView.canGoBack()返回的是一个Boolean类型，所以我们把它返回为true
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebView != null && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onSupportNavigateUp() {
        if (mWebView != null && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onSupportNavigateUp();
    }
}
